package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.param.UserLoginParam;
import com.ketangpai.admin.service.UserService;
import com.ketangpai.admin.utils.JsonResult;
import com.ketangpai.admin.utils.JwtUtil;
import com.ketangpai.admin.utils.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    @ResponseBody
    public JsonResult<?> login(@RequestBody UserLoginParam user) throws Exception {
        User loginUser = userService.findByUsername(user.getAccount());
        if (loginUser == null){
            return JsonResult.error("用户名不存在");
        }else if (Md5Util.getMD5String(user.getPassword()).equals(loginUser.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("account", loginUser.getAccount());
            claims.put("id", loginUser.getId());
            String token = JwtUtil.genToken(claims);
            System.out.println(token);
            System.out.println(JwtUtil.parseToken(token));
            loginUser.setToken(token);

            return JsonResult.success(loginUser, "登录成功");
        } else
            return JsonResult.error("密码错误");
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public JsonResult<?> register(@RequestBody User adminUser) {
        //查询用户名是否已存在
        User userByName = userService.findByUsername(adminUser.getAccount());
        //查询手机号是否已注册
        User userByPhone = userService.findByPhone(adminUser.getPhone());
        System.out.println(userByPhone);
        System.out.println(userByName);
        //注册
        if (userByName == null && userByPhone == null) {
            //注册
            try {
                userService.register(adminUser);
                return JsonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                return JsonResult.error("注册失败");
            }
        } else if (userByPhone != null){
            //占用
            return JsonResult.error("手机号已注册");
        }else if (userByName != null){
            //占用
            return JsonResult.error("用户名已存在");
        }else
            return JsonResult.error("注册失败");
    }

    @ApiOperation("根据id查询")
    @GetMapping("/selectById/{id}")
    public JsonResult<?> selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return JsonResult.success(user);
    }
    @ApiOperation("根据账号查询")
    @GetMapping("/selectByAccount/{account}")
    public JsonResult<?> selectByAccount(@PathVariable String account) {
        User user = userService.selectByAccount(account);
        return JsonResult.success(user);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public JsonResult<?> update(@RequestBody User user) {
        System.out.println("个人信息: \n"+user);
        boolean b= userService.update(user);
        if (b) {
            return JsonResult.success("编辑成功");
        } else {
            return JsonResult.error("编辑失败");
        }
    }
}
